import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

public class Solution968 {
    public int minCameraCover(TreeNode root) {
        return dfs(root)[1];
    }

    public int[] dfs (TreeNode node) {
        if (node == null) {
            return new int[]{Integer.MAX_VALUE, 0, 0};
        }
        int[] leftArray = dfs(node.left);
        int[] rightArray = dfs(node.right);
        int[] array = new int[3];
        array[0] = leftArray[2] + rightArray[2] + 1;
        array[1] = Math.min(array[0], Math.min(leftArray[0] + rightArray[1], rightArray[0] + leftArray[1]));
        array[2] = Math.min(array[0], leftArray[1] + rightArray[1]);
        return array;
    }
}
